package com.ruoyi.web.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.entity.sealentity.Seal;
import com.ruoyi.common.entity.sealentity.SealPermission;
import com.ruoyi.common.mapper.SealMapper;
import com.ruoyi.common.mapper.UtilsMapper;
import com.ruoyi.common.utils.LogUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.common.utils.wst.DateUtil;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.web.service.SealService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class SealServiceImpl implements SealService {

    @Autowired
    private SealMapper sealMapper;

    @Autowired
    private UtilsMapper utilsMapper;

    @Autowired
    private SysDeptMapper deptMapper;

    /**
     * 查看印章列表
     * @param seal
     * @return
     */
    @DataScope(deptAlias = "d")
    @Override
    public List<Seal> getSealList(Seal seal) {
        List<Seal> list = sealMapper.getSealList(seal);
        List<Seal> displayList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getDeptName()!=null){ //数据过滤，查不到其他部门的详细信息，但会查到印章的信息，=》判断是否有部门的详细信息，把它去除
                displayList.add(list.get(i));
            }
        }
        return displayList;
    }

    /**
     * 新增印章
     * @param seal
     * @return
     */
    @Override
    public int addSeal(Seal seal) {
        /**
         * id parentId createBy createTime updateTime status 内部创建参数
         * name sealData deptId 前端传输参数
         */
        seal.setId(IdUtils.simpleUUID());
        seal.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        seal.setCreateTime(new Date());
        seal.setUpdateTime(new Date());
        seal.setStatus("0");
        /**
         * 装parentId
         */
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(seal.getDeptId());
        SysDept dept = utilsMapper.getDeptList(sysDept);
        seal.setParentId(dept.getParentId());

        return sealMapper.addSeal(seal);
    }

    /**
     * 修改印章
     * @param seal
     * @return
     */
    @Override
    public int updateSeal(Seal seal) {
        /**
         *id name deptId sealData status 前端传输参数
         * parentId updateTime 内部创建参数
         */
        seal.setUpdateTime(new Date());
        /**
         * 装parentId
         */
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(seal.getDeptId());
        SysDept dept = utilsMapper.getDeptList(sysDept);
        seal.setParentId(dept.getParentId());
        return sealMapper.updateSeal(seal);
    }

    /**
     * 删除印章
     * @param id
     * @return
     */
    @Override
    public int deleteSeal(String id) {
        return sealMapper.deleteSeal(id);
    }

    /**
     * 印章授权
     * @param ids 用户id数组
     * @param sealPermission 授权详细信息
     * @return
     */
    @Transactional
    @Override
    public int AuthSeal(Long[] ids,SealPermission sealPermission) {
        /**
         * 印章id 状态 =>前端穿传送
         * 授权人 授权时间  更新者 更新时间=>固定值
         * 授权类型 权限时间 =>两种情况
         * 用户id =>循环
         */
        sealPermission.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        if (sealPermission.getType()==-1){
            sealPermission.setPermissionTime(DateUtil.addYear(new Date(),999));
        }else {
            Date permissionTime = sealPermission.getPermissionTime();
            System.out.println(permissionTime);
        }
        for (Long id : ids) {
            try {
                sealPermission.setUpdateBy(null);
                sealPermission.setUpdateTime(null);
                sealPermission.setId(IdUtils.simpleUUID());
                sealPermission.setUserId(id);
                sealPermission.setAuthorizationTime(new Date());
                sealMapper.addAuthSeal(sealPermission);
            }catch (Exception e){
                LogUtils.getBlock(e);
                e.printStackTrace();
                return 0;
            }
        }
        return 1;
    }

    /**
     * 查看印章权限列表
     * @param sealPermission
     * @return
     */
    @DataScope(deptAlias = "d")
    @Override
    public List<SealPermission> AuthSealQuery(SealPermission sealPermission) {
        List<SealPermission> queryList = sealMapper.AuthSealQuery(sealPermission);
        List<SealPermission> displayList = new ArrayList<>();
        for (int i = 0; i < queryList.size(); i++) {
            try {
                if (queryList.get(i).getDeptName()!=null){
                    displayList.add(queryList.get(i));
                }
            }catch (Exception e){
                e.printStackTrace();
                LogUtils.getBlock(e);
            }
        }
        return displayList;
    }

    /**
     * 编辑用户授权信息
     * @param sealPermission
     * @return
     */
    @Override
    public int editAuthSeal(SealPermission sealPermission) {
        sealPermission.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        sealPermission.setUpdateTime(new Date());
        return sealMapper.editAuthSeal(sealPermission);
    }

    /**
     * 删除用户印章权限
     * @param id
     * @return
     */
    @Override
    public int deleteAuthSeal(String id) {
        return sealMapper.deleteAuthSeal(id);
    }
}
